/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.ReservationController;
import Controller.SejourController;
import Model.Agenda;
import Model.Categorie;
import Model.Chambre;
import Model.Sejour;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.util.Date;
import java.util.LinkedList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Hugo
 */
public class FournirChambre extends AbstractViewPanel{
    private SejourController _controller;
    private int _idSejour;
    private Date _dteDebut;
    private Date _dteFin;
    private String _nomCategory;
    private int _nbrChambre;
    
    public FournirChambre(SejourController controller, Date dteDebut, Date dteFin, int idSejour) {
        this._controller = controller;
        this._dteDebut = dteDebut;
        this._dteFin = dteFin;
        this._nbrChambre = 0;
        this._nomCategory = "";
        this._controller.setDteDebut(dteDebut);
        this._controller.setDteFin(dteFin);
        this._idSejour = idSejour;
        initComponents();
        populerTable();
        PeuplerTblChambre(Agenda.getInstance().lireChambreSejour(idSejour));
    }
    
    public FournirChambre(SejourController controller, Date dteDebut, Date dteFin, int nbrChambre, String nomCategory, int idSejour) {
        this._controller = controller;
        this._dteDebut = dteDebut;
        this._dteFin = dteFin;
        this._nbrChambre = nbrChambre;
        this._nomCategory = nomCategory;
        this._controller.setDteDebut(dteDebut);
        this._controller.setDteFin(dteFin);
        this._idSejour = idSejour;
        initComponents();
        populerTable();
        PeuplerTblChambre(Agenda.getInstance().lireChambreSejour(idSejour));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblChambreLibre = new javax.swing.JTable();
        btnAjoutSejour = new javax.swing.JButton();
        btnDemarrerSejour = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblChambreChoisi = new javax.swing.JTable();

        tblChambreLibre.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Catégorie", "Nb de chambres libre"
            }
        ));
        jScrollPane1.setViewportView(tblChambreLibre);

        btnAjoutSejour.setText("Ajouter au séjour");
        btnAjoutSejour.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAjoutSejourMouseClicked(evt);
            }
        });

        btnDemarrerSejour.setText("Démarrer le séjour");
        btnDemarrerSejour.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDemarrerSejourMouseClicked(evt);
            }
        });

        tblChambreChoisi.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(tblChambreChoisi);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btnAjoutSejour)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(37, 37, 37)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(26, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnDemarrerSejour)
                                .addGap(64, 64, 64))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAjoutSejour))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnDemarrerSejour)))
                .addContainerGap(55, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAjoutSejourMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAjoutSejourMouseClicked
        //trouver la rangée sélectionner
        int[] row_indexes= tblChambreLibre.getSelectedRows();
        String numChambre = "";
        String nomCategorie = "";
        for(int i=0;i<row_indexes.length;i++){
            numChambre = tblChambreLibre.getValueAt(row_indexes[i], 0).toString();  
            nomCategorie = tblChambreLibre.getValueAt(row_indexes[i], 1).toString(); 
        }

        //ajouter la rangé
        if(nomCategorie.equals("") || numChambre.equals("")){
            JOptionPane.showMessageDialog(this,"Veuillez choisir une chambre.");
        }
        else{
            Chambre c = new Chambre(new Categorie(nomCategorie),Integer.parseInt(numChambre));
            this._controller.addChambre(c);
        }
    }//GEN-LAST:event_btnAjoutSejourMouseClicked

    private void btnDemarrerSejourMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDemarrerSejourMouseClicked
        JOptionPane.showMessageDialog(this, "Le séjour " + Integer.toString(this._idSejour) + " à été démarré.");
        this.getTopLevelAncestor().setVisible(false);
    }//GEN-LAST:event_btnDemarrerSejourMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAjoutSejour;
    private javax.swing.JButton btnDemarrerSejour;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tblChambreChoisi;
    private javax.swing.JTable tblChambreLibre;
    // End of variables declaration//GEN-END:variables

    private void populerTable(){
        String[][] listeChambre = null;
         
        if(this._nomCategory.equals("")){
            listeChambre = Agenda.getInstance().AfficherChambreLibreParCategorie(_dteDebut, _dteFin);
        }
        else{
            Categorie cat = new Categorie(this._nomCategory);
            listeChambre = Agenda.getInstance().AfficherChambreLibreParCategorie(_dteDebut, _dteFin, cat);
        }
         
        String[] celNames;
        
        celNames = new String[2];
        celNames[0] = "Numéro de la chambre";
        celNames[1] = "Catégorie";

        JTable jtbl = new JTable(listeChambre, celNames);
        this.tblChambreLibre.setModel(new DefaultTableModel());
        this.tblChambreLibre.setModel(jtbl.getModel());        
    }
    
    
    private void PeuplerTblChambre(String[][] listeChambre){
        //String[][] listeChambre = null;
        //listeChambre = Agenda.getInstance().lireChambreSejour(idSejour);
        
        String[] celNames;
        
        celNames = new String[2];
        celNames[0] = "Numéro de la chambre";
        celNames[1] = "Catégorie";

        
        JTable jtbl = new JTable(listeChambre, celNames);
        this.tblChambreChoisi.setModel(new DefaultTableModel());
        this.tblChambreChoisi.setModel(jtbl.getModel());     
    }
    
    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        switch(evt.getPropertyName()){
            case SejourController.DTEFIN:
                //the num of confirmation has been attributed to the reservation so its finished.
                break;
            case SejourController.DTEDEBUT:
                //the num of confirmation has been attributed to the reservation so its finished.
                this.getTopLevelAncestor().setVisible(false);                
                break;
            case SejourController.ADD_CHAMBRE:
                populerTable();
                PeuplerTblChambre((String[][])evt.getNewValue());
                break;
            case SejourController.SEJOUR_PROPERTY:
                
                
                break;
        }
    }
}
